const mongoose = require('mongoose');
require('dotenv').config();

async function checkImages() {
  try {
    await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/jpshop');
    console.log('MongoDB连接成功');

    const Product = require('./src/models/product.model');
    const products = await Product.find({}).limit(5).select('name mainImage images');

    console.log('=== 商品图片信息详细分析 ===');
    products.forEach((product, index) => {
      console.log(`\n${index + 1}. 商品名称: ${product.name}`);
      console.log(`   mainImage: "${product.mainImage}"`);
      console.log(`   mainImage长度: ${product.mainImage ? product.mainImage.length : 0}`);

      if (product.mainImage) {
        console.log(`   mainImage前50字符: "${product.mainImage.substring(0, 50)}"`);
        if (product.mainImage.length > 50) {
          console.log(`   mainImage后50字符: "${product.mainImage.substring(product.mainImage.length - 50)}"`);
        }
      }

      console.log(`   images数组长度: ${product.images ? product.images.length : 0}`);
      if (product.images && product.images.length > 0) {
        product.images.forEach((img, imgIndex) => {
          console.log(`     图片${imgIndex + 1}: "${img}"`);
          console.log(`     长度: ${img ? img.length : 0}`);
          if (img && img.length > 50) {
            console.log(`     前50字符: "${img.substring(0, 50)}"`);
            console.log(`     后50字符: "${img.substring(img.length - 50)}"`);
          }
        });
      }
      console.log('   ---');
    });

    // 检查Banner图片
    console.log('\n=== Banner图片信息 ===');
    const Banner = require('./src/models/Banner');
    const banners = await Banner.find({}).limit(3).select('title image');

    banners.forEach((banner, index) => {
      console.log(`\n${index + 1}. Banner标题: ${banner.title}`);
      console.log(`   image: "${banner.image}"`);
      console.log(`   image长度: ${banner.image ? banner.image.length : 0}`);

      if (banner.image && banner.image.length > 50) {
        console.log(`   前50字符: "${banner.image.substring(0, 50)}"`);
        console.log(`   后50字符: "${banner.image.substring(banner.image.length - 50)}"`);
      }
    });

    await mongoose.disconnect();
    console.log('\n数据库连接已关闭');
  } catch (err) {
    console.error('错误:', err);
    process.exit(1);
  }
}

checkImages();
